const app = getApp()
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['海南省', '海口市', '秀英区'],
    provinceName: '海南省',
    cityName: '海口市',
    districtName: '秀英区'
  },
  // 地址选择
  bindRegionChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value,
      provinceName: e.detail.value[0],
      cityName: e.detail.value[1],
      districtName: e.detail.value[2]
    })
  },
  //收货人姓名
  addressNameInput: function (e) {
    this.setData({
      addressName: e.detail.value
    })
  },
  //收货人手机号
  addressMobileInput: function (e) {
    this.setData({
      addressMobile: e.detail.value
    })
  },
  //详细地址
  addressDescInput: function (e) {
    this.setData({
      addressDesc: e.detail.value
    })
  },
  checkboxChange:function(e){
    this.setData({
      isDefault: e.detail.value.length
    })
  },
  //提交地址
  confirmAddress: function () {
    let that = this
    let memberId = app.globalData.userInfo.id
    let receiveName = this.data.addressName
    if (util.isBlank(receiveName)) {
      if (!util.isBlank(this.data.defaultAddress)) {
        receiveName = this.data.defaultAddress.receiveName
      }
      if (util.isBlank(receiveName)) {
        wx.showToast({
          title: '请输入姓名',
          icon: 'none',
          duration: 1500
        })
        return
      }
    }

    let receiveMobile = this.data.addressMobile
    if (util.isBlank(receiveMobile)) {
      if (!util.isBlank(this.data.defaultAddress)) {
        receiveMobile = this.data.defaultAddress.receiveMobile
      }
      if (util.isBlank(receiveMobile)) {
        wx.showToast({
          title: '请输入电话',
          icon: 'none',
          duration: 1500
        })
        return
      }
    }
    if (!util.validatemobile(receiveMobile)) {
      return
    }
    let receiveAddress = this.data.addressDesc
    if (util.isBlank(receiveAddress)) {
      if (!util.isBlank(this.data.defaultAddress)) {
        receiveAddress = this.data.defaultAddress.receiveAddress
      }
      if (util.isBlank(receiveAddress)) {
        wx.showToast({
          title: '请输入详细地址',
          icon: 'none',
          duration: 1500
        })
        return
      }
    }


    let provinceName = that.data.provinceName
    if (util.isBlank(provinceName)) {
      if (!util.isBlank(this.data.defaultAddress)) {
        provinceName = this.data.defaultAddress.provinceName
      }
      if (util.isBlank(provinceName)) {
        wx.showToast({
          title: '请选择省份',
          icon: 'none',
          duration: 1500
        })
        return
      }
    }
    let cityName = that.data.cityName
    if (util.isBlank(cityName)) {
      if (!util.isBlank(this.data.defaultAddress)) {
        cityName = this.data.defaultAddress.cityName
      }
      if (util.isBlank(cityName)) {
        wx.showToast({
          title: '请选择城市',
          icon: 'none',
          duration: 1500
        })
        return
      }
    }
    let districtName = that.data.districtName

    if (util.isBlank(districtName)) {
      if (!util.isBlank(this.data.defaultAddress)) {
        districtName = this.data.defaultAddress.districtName
      }
      if (util.isBlank(cityName)) {
        wx.showToast({
          title: '请选择区县',
          icon: 'none',
          duration: 1500
        })
        return
      }
    }
    let isDefault = that.data.isDefault
    let id = 0
    if (!util.isBlank(this.data.defaultAddress)) {
      id = this.data.defaultAddress.id
    }
    //提交地址
    wx.request({
      url: 'https://small.qiongyazuche.com/sunday/small/address/save',
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        "id": id,
        "memberId": memberId,
        "receiveName": receiveName,
        "receiveMobile": receiveMobile,
        "receiveAddress": receiveAddress,
        "provinceName": provinceName,
        "cityName": cityName,
        "districtName": districtName,
        "isDefault": isDefault
      },
      success: function (res) {
        if (res.data.code != 0) {
          wx.showToast({
            title: '系统错误',
            icon: 'none',
            duration: 1500
          })
        } else {
          wx.redirectTo({
            url: '../user-address/user-address?startTime=' + that.data.startTime + '&endTime=' + that.data.endTime + '&day=' + that.data.day + '&startHour=' + that.data.startHour + '&endHour=' + that.data.endHour + '&productId=' + that.data.productId + '&startCityId=' + that.data.startCityId + '&endCityId=' + that.data.endCityId + '&flag=' + that.data.flag + '&startCityName=' + that.data.startCityName + '&endCityName=' + that.data.endCityName + '&sendType=' + that.data.sendType + '&sendName=' + that.data.sendName + '&sendMobile=' + that.data.sendMobile + '&sendAddress=' + that.data.sendAddress + '&receiveType=' + that.data.receiveType + '&receiveName=' + that.data.receiveName + '&receiveMobile=' + that.data.receiveMobile + '&receiveAddress=' + that.data.receiveAddress + '&type=' + that.data.type
          })
        }
      }
    })


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      productId: options.productId ? options.productId : '',
      startTime: options.startTime ? options.startTime : '',
      startHour: options.startHour ? options.startHour : '',
      endTime: options.endTime ? options.endTime : '',
      endHour: options.endHour ? options.endHour : '',
      day: options.day ? options.day : '',
      startCityId: options.startCityId ? options.startCityId : '',
      startCityName: options.startCityName ? options.startCityName : '',
      endCityId: options.endCityId ? options.endCityId : '',
      endCityName: options.endCityName ? options.endCityName : '',
      sendType: options.sendType ? options.sendType : '',
      sendName: options.sendName ? options.sendName : '',
      sendMobile: options.sendMobile ? options.sendMobile : '',
      sendAddress: options.sendAddress ? options.sendAddress : '',
      receiveType: options.receiveType ? options.receiveType : '',
      receiveName: options.receiveName ? options.receiveName : '',
      receiveMobile: options.receiveMobile ? options.receiveMobile : '',
      receiveAddress: options.receiveAddress ? options.receiveAddress : '',
      flag: options.flag ? options.flag : '',
      type: options.type ? options.type : ''
    })

    if (options.id){
      let that = this
      wx.request({
        url: 'https://small.qiongyazuche.com/sunday/small/address/input',
        method: "POST",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: {
          "id": options.id
        },
        success: function (res) {
          if (res.data.code != 0) {
            wx.showToast({
              title: '系统错误',
              icon: 'none',
              duration: 1500
            })
          } else {
            that.setData({
              defaultAddress: res.data.result,
              addressDesc: res.data.result.receiveAddress,
              region: [res.data.result.provinceName, res.data.result.cityName, res.data.result.districtName],
            })
            //console.log("选择的地址"+that.data.defaultAddress.id)
          }

        }
      })
    }
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '琼崖租车',
      path: '/pages/index/index'
    }
  }
})